import pyautogui
import random
import time
from typing import Literal

class MouseMover:
    def __init__(self):
        self.screen_w, self.screen_h = pyautogui.size()
        self.move_interval = 5  # 默认移动间隔(秒)
        self.brownian_step = 500   # 布朗运动步长
        self.running = False
        self.safe_margin = 50     # 屏幕边缘安全距离

    def _clamp_position(self, x: int, y: int) -> tuple[int, int]:
        """限制坐标在屏幕范围内"""
        x = max(self.safe_margin, min(x, self.screen_w - self.safe_margin))
        y = max(self.safe_margin, min(y, self.screen_h - self.safe_margin))
        return x, y

    def random_jump(self):
        """模式1: 全屏随机坐标跳跃"""
        x = random.randint(self.safe_margin, self.screen_w - self.safe_margin)
        y = random.randint(self.safe_margin, self.screen_h - self.safe_margin)
        pyautogui.moveTo(x, y, duration=0.25)
        time.sleep(self.move_interval)

    def brownian_move(self):
        """模式2: 布朗运动模式"""
        current_x, current_y = pyautogui.position()
        dx = random.randint(-self.brownian_step, self.brownian_step)
        dy = random.randint(-self.brownian_step, self.brownian_step)
        new_x, new_y = self._clamp_position(current_x + dx, current_y + dy)
        pyautogui.moveTo(new_x, new_y, duration=0.2)
        time.sleep(self.move_interval)

    def start(self, mode: Literal['jump', 'brownian'] = 'brownian'):
        self.running = True
        while self.running:
            if mode == 'jump':
                self.random_jump()
            else:
                self.brownian_move()

    def stop(self):
        self.running = False


def main():
    mover = MouseMover()
    print("开始鼠标随机移动(按Ctrl+C停止)...")
    mover.start()

if __name__ == "__main__":
    while True:
        try:
            main()
        except Exception as e:
            print(e)
            time.sleep(60*10)